package ru.niir.goodfil.ui;

import ru.niir.goodfil.db.Good;
import ru.niir.goodfil.db.GoodAbsence;
import ru.niir.goodfil.db.GoodType;
import ru.niir.goodfil.exceptions.ValidationException;

import java.util.*;

public class GoodAbsenceReportBean extends PageBean{

    private boolean allRows;

    private int rowsCount;

    private Date beginDate;

    private Date endDate;

    private Good good;

    public GoodAbsenceReportBean() {
        init();
    }

    public void init(){
        allRows = false;
        rowsCount = 28;
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        beginDate = calendar.getTime();
        endDate = new Date();
        good = new Good();
    }

    public List<Good> getGoods() throws ValidationException {
        List<GoodType> goodTypes = gf().getGoodTypes();
        List<Good> goods = gf().getGoods(getSessionBean().getGoodsFilter());
        List<Good> result = new ArrayList<Good>();
        List<GoodAbsence> list = gf().getGoodAbsence(beginDate, endDate);

        for (Good good : goods) {
            if (good.isDeleted()) {
                continue;
            }

            for (GoodType goodType : goodTypes) {
                if (goodType.getId() == good.getGoodTypeId()) {
                    good.setGoodType(goodType);
                }
            }
            //  Получение данных о том, выбран ли данный товар
            good.setSelected(getSessionBean().getSelectedGoods().contains(good.getId()));
            for(GoodAbsence goodAbsence : list){

                if(good.getId() == goodAbsence.getIdGood()){
                    good.setGoodAbsenceCount(goodAbsence.getCount());
                    good.setOrdersCount(goodAbsence.getOrdersCount());
                    break;
                }
            }
            if(good.getGoodAbsenceCount()!=0){
                result.add(good);
            }
        }
        return result;
    }

    public void showAllRows() {
        if (!isAllRows()) {
            try {
                setRowsCount(getGoods().size());
                setAllRows(true);
            } catch (ValidationException e) {
                e.printStackTrace();
            }
        } else {
            setAllRows(false);
            setRowsCount(28);
        }
    }

    public boolean isAllRows() {
        return allRows;
    }

    public void setAllRows(boolean allRows) {
        this.allRows = allRows;
    }

    public int getRowsCount() {
        return rowsCount;
    }

    public void setRowsCount(int rowsCount) {
        this.rowsCount = rowsCount;
    }

    public Date getBeginDate() {
        if(beginDate == null){
            return null;
        }
        else return new Date(beginDate.getTime());
    }

    public void setBeginDate(Date beginDate) {
        if (beginDate != null) {
            this.beginDate = beginDate;
        } else {
            this.beginDate = null;
        }
    }

    public Date getEndDate() {
        if(endDate == null){
            return null;
        }
        else return new Date(endDate.getTime());
    }

    public void setEndDate(Date endDate) {
        if (endDate != null) {
            this.endDate = endDate;
        } else {
            this.endDate = null;
        }
    }

    public Good getGood() {
        return good;
    }

    public void setGood(Good good) {
        this.good = good;
    }
}
